package com.hertz.iris.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.hertz.iris.R;
import com.hertz.iris.ui.IrisFragmentActivity;
import com.hertz.iris.ui.IrisMainPagerAdapter;
import com.hertz.iris.ui.ItemListFragment;
import com.hertz.iris.view.alarm.NormalAlarmActivity;
import com.hertz.iris.view.setting.SettingActivity;
import com.umeng.update.UmengUpdateAgent;
import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends IrisFragmentActivity {

	private TabPageIndicator indicator;
    private ViewPager pager;
    private boolean onPause;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    	UmengUpdateAgent.update(this);
    	super.onCreate(savedInstanceState);
    	
    	initView();
        
    }

    private void initView(){
      setContentView(R.layout.main);
      initScreen();
    }
    
    private void initScreen() {
    	indicator = (TabPageIndicator)findViewById(R.id.tpi_header);
    	pager = (ViewPager)findViewById(R.id.vp_pages);
    	pager.setAdapter(new IrisMainPagerAdapter(getResources(), getSupportFragmentManager()));
        indicator.setViewPager(pager);
        pager.setCurrentItem(0);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.new_normal_alarm:
            	Intent newAlarmIntent = new Intent(this, NormalAlarmActivity.class);
                startActivity(newAlarmIntent);
                return true;
            case R.id.refresh:
            	int index = pager.getCurrentItem();
    			refresh(index);
    			
    			
                return true;
            case R.id.action_settings:
            	Intent settingAlarmIntent = new Intent(this, SettingActivity.class);
                startActivity(settingAlarmIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		onPause = true;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(onPause){
			int index = pager.getCurrentItem();
			refresh(index);
		}
		onPause = false;
	}
 
	private void refresh(int index){
		if(index != 1){
			ItemListFragment ilf = (ItemListFragment)getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.vp_pages+":" + index);
	    	ilf.refresh();
		}else{
			MatchsFrag matchsFrag = (MatchsFrag)getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.vp_pages+":" + index);
			int matchsAdapterIndex = matchsFrag.getCurrentIndex();
			ItemListFragment ilf = (ItemListFragment)matchsFrag.getChildFragmentManager().findFragmentByTag("android:switcher:"+R.id.match_list_pages+":" + matchsAdapterIndex);
	    	ilf.refresh();
		}
	}
    
}
